package com.hspedu.seckill.validator;

import com.hspedu.seckill.util.ValidatorUtil;
import org.springframework.util.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class IsMobileValidator implements ConstraintValidator<IsMobile,String> {

    private boolean required = true;

    @Override
    public void initialize(IsMobile constraintAnnotation) {
        required = constraintAnnotation.required();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {

        //非必填
        if (!required){
            if (!StringUtils.hasText(value)){
                return true;
            }else {
                return ValidatorUtil.isMobile(value);
            }
        }

        return ValidatorUtil.isMobile(value);
    }

}
